package tech.aistar.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tech.aistar.exception.MyException;
import tech.aistar.pojo.User;

import java.util.Arrays;
import java.util.List;

/**
 * 本类用来演示: 参数的绑定
 *
 * @author: success
 * @date: 2021/5/10 9:04 上午
 */
@Controller
@RequestMapping("/day02")
public class ParamController {

    //简单类型 - 基本数据类型/包装类型+String类型+Date类型
    //method不写 - @RequestMapping接受任何请求
    //@RequestMapping(value = "/param01.action",method = RequestMethod.GET)
    @GetMapping(value = "/param01.action")
    @ResponseBody//返回结果 - json字符串
    public User test01(Integer id,String name){
        System.out.println("id:"+id+",name:"+name);
        return new User(id,name);
    }

    /**
     * @RequestParam
     * 1. 当请求的key和方法的参数不一致的时候.
     * 2. required - 该参数必须要传入
     * 3. defaultValue - 如果该参数没有传入值,给定一个默认值
     *
     * @param id
     * @param name
     * @return
     */
    @GetMapping(value = "/param02.action")
    @ResponseBody//返回结果 - json字符串
    public User test02(@RequestParam(value = "sid",defaultValue = "100") Integer id,@RequestParam("username") String name){
        System.out.println("id:"+id+",name:"+name);
        return new User(id,name);
    }

    /**
     * 参数绑定 - 自定义的数据类型
     * - 演示了日期类型的绑定
     * @return
     */
    @GetMapping(value = "/param03.action")
    @ResponseBody//返回结果 - json字符串
    public User test03(User user) {
        return user;
    }

    /**
     * http://localhost:8888/mvc/day02/param04.action?ids=100&ids=200&ids=500
     * @param ids
     * @return
     */
    @GetMapping(value = "/param04.action")
    @ResponseBody//返回结果 - json字符串
    public String test03(Long[] ids) {
        return "ids:"+ Arrays.toString(ids);
    }

    /**
     * http://localhost:8888/mvc/day02/param05.action?ids=100&ids=200&ids=500
     * 如果使用一个集合来接受同个key对应的多个参数的话,必须加上@RequestParam
     * @param ids
     * @return
     */
    @GetMapping(value = "/param05.action")
    @ResponseBody//返回结果 - json字符串
    public String test03(@RequestParam("ids") List<Long> ids) {
        return "ids:"+ ids;
    }

    /**
     * 支持级联属性的绑定 - 绑定的单个对象
     * http://localhost:8888/mvc/day02/param06.action?id=100&userInfo.info=这是一个好人
     * @param user
     * @return
     */
    @GetMapping(value = "/param06.action")
    @ResponseBody//返回结果 - json字符串
    public User test04(User user) {
        System.out.println(10/0);
        return user;
    }

    /**
     * 级联属性 - 绑定到集合对象 addressList[0].info
     * http://localhost:8888/mvc/day02/param07.action?id=100&userInfo.info=%E8%BF%99%E6%98%AF%E4%B8%80%E4%B8%AA%E5%A5%BD%E4%BA%BA&addressList[0].province=%E6%B1%9F%E8%8B%8F%E7%9C%81&addressList[1].province=%E5%AE%89%E5%BE%BD%E7%9C%81
     * @param user
     * @return
     */
    @GetMapping(value = "/param07.action")
    @ResponseBody//返回结果 - json字符串
    public User test05(User user) throws MyException {
        if(user.getId()==100){
            throw new MyException("id不合法!");
        }
        return user;
    }
}
